package com.spelet.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spelet.Main;
import com.badlogic.gdx.math.MathUtils;
import com.spelet.StaticTextures;
import com.spelet.VariableCollection;
import com.spelet.screens.Game;
import com.spelet.screens.CustomGame;
import com.spelet.screens.MapEditor;

public class Character {
	private Sprite[] charStill;
	private Sprite[] charMoveDown;
	private Sprite[] charMoveUp;
	private Sprite[] charMoveRight;
	private Sprite[] charMoveLeft;
	private Sprite[] hitBlockRight;
	private Sprite[] hitBlockLeft;
	private Sprite[] kickBlockRight;
	private Sprite[] kickBlockLeft;
	private Sprite[] fallDownHole;
	private Sprite[] hitBlockUp;
	private Sprite[] kickBlockDown;
	private Sprite spriteToDraw;
	private int moveTimer;
	private int animStep;
	private int animStep2;
	public int currentState;
	public boolean locked;
	public int tileX, tileY, spriteX, spriteY;
	boolean right, left, down;
	boolean up;
	public int redKeys,blueKeys, yellowKeys;
	public Character() {
		animStep = 0;
		charStill = new Sprite[1];
		charStill[0] = StaticTextures.character.createSprite("hatmanfront");
		charMoveDown = new Sprite[3];
		charMoveDown[0] = StaticTextures.character.createSprite("movefront0");
		charMoveDown[1] = StaticTextures.character.createSprite("movefront1");
		charMoveDown[2] = StaticTextures.character.createSprite("movefront2");
		charMoveUp = new Sprite[3];
		charMoveUp[0] = StaticTextures.character.createSprite("moveup0");
		charMoveUp[1] = StaticTextures.character.createSprite("moveup1");
		charMoveUp[2] = StaticTextures.character.createSprite("moveup2");
		charMoveRight = new Sprite[4];
		charMoveRight[0] = StaticTextures.character.createSprite("hatmanright");
		charMoveRight[1] = StaticTextures.character.createSprite("runright1");
		charMoveRight[2] = StaticTextures.character.createSprite("runright2");
		charMoveRight[3] = StaticTextures.character.createSprite("runright3");
		charMoveLeft = new Sprite[4];
		charMoveLeft[0] = StaticTextures.character.createSprite("hatmanleft");
		charMoveLeft[1] = StaticTextures.character.createSprite("runleft1");
		charMoveLeft[2] = StaticTextures.character.createSprite("runleft2");
		charMoveLeft[3] = StaticTextures.character.createSprite("runleft3");
		hitBlockRight = new Sprite[9];
		hitBlockRight[0] = StaticTextures.hitBlock.createSprite("hitblockright0");
		hitBlockRight[1] = StaticTextures.hitBlock.createSprite("hitblockright1");
		hitBlockRight[2] = StaticTextures.hitBlock.createSprite("hitblockright2");
		hitBlockRight[3] = StaticTextures.hitBlock.createSprite("hitblockright3");
		hitBlockRight[4] = StaticTextures.hitBlock.createSprite("hitblockright4");
		hitBlockRight[5] = StaticTextures.hitBlock.createSprite("hitblockright5");
		hitBlockRight[6] = StaticTextures.hitBlock.createSprite("hitblockright6");
		hitBlockRight[7] = StaticTextures.hitBlock.createSprite("hitblockright7");
		hitBlockRight[8] = StaticTextures.hitBlock.createSprite("hitblockright8");
		hitBlockLeft = new Sprite[9];
		hitBlockLeft[0] = StaticTextures.hitBlock.createSprite("hitblockleft0");
		hitBlockLeft[1] = StaticTextures.hitBlock.createSprite("hitblockleft1");
		hitBlockLeft[2] = StaticTextures.hitBlock.createSprite("hitblockleft2");
		hitBlockLeft[3] = StaticTextures.hitBlock.createSprite("hitblockleft3");
		hitBlockLeft[4] = StaticTextures.hitBlock.createSprite("hitblockleft4");
		hitBlockLeft[5] = StaticTextures.hitBlock.createSprite("hitblockleft5");
		hitBlockLeft[6] = StaticTextures.hitBlock.createSprite("hitblockleft6");
		hitBlockLeft[7] = StaticTextures.hitBlock.createSprite("hitblockleft7");
		hitBlockLeft[8] = StaticTextures.hitBlock.createSprite("hitblockleft8");
		hitBlockUp = new Sprite[8];
		hitBlockUp[0] = StaticTextures.hitBlock.createSprite("hitblockup0");
		hitBlockUp[1] = StaticTextures.hitBlock.createSprite("hitblockup1");
		hitBlockUp[2] = StaticTextures.hitBlock.createSprite("hitblockup2");
		hitBlockUp[3] = StaticTextures.hitBlock.createSprite("hitblockup3");
		hitBlockUp[4] = StaticTextures.hitBlock.createSprite("hitblockup4");
		hitBlockUp[5] = StaticTextures.hitBlock.createSprite("hitblockup5");
		hitBlockUp[6] = StaticTextures.hitBlock.createSprite("hitblockup6");
		hitBlockUp[7] = StaticTextures.hitBlock.createSprite("hitblockup7");
		kickBlockDown = new Sprite[6];
		kickBlockDown[0] = StaticTextures.kickBlock.createSprite("kickblockdown0");
		kickBlockDown[1] = StaticTextures.kickBlock.createSprite("kickblockdown1");
		kickBlockDown[2] = StaticTextures.kickBlock.createSprite("kickblockdown2");
		kickBlockDown[3] = StaticTextures.kickBlock.createSprite("kickblockdown3");
		kickBlockDown[4] = StaticTextures.kickBlock.createSprite("kickblockdown4");
		kickBlockDown[5] = StaticTextures.kickBlock.createSprite("kickblockdown5");
		kickBlockRight = new Sprite[9];
		kickBlockRight[0] = StaticTextures.kickBlock.createSprite("hatmanroundkick1");
		kickBlockRight[1] = StaticTextures.kickBlock.createSprite("hatmanroundkick2");
		kickBlockRight[2] = StaticTextures.kickBlock.createSprite("hatmanroundkick3");
		kickBlockRight[3] = StaticTextures.kickBlock.createSprite("hatmanroundkick4");
		kickBlockRight[4] = StaticTextures.kickBlock.createSprite("hatmanroundkick5");
		kickBlockRight[5] = StaticTextures.kickBlock.createSprite("hatmanroundkick6");
		kickBlockRight[6] = StaticTextures.kickBlock.createSprite("hatmanroundkick7");
		kickBlockRight[7] = StaticTextures.kickBlock.createSprite("hatmanroundkick8");
		kickBlockRight[8] = StaticTextures.kickBlock.createSprite("hatmanroundkick9");
		kickBlockLeft = new Sprite[9];
		kickBlockLeft[0] = StaticTextures.kickBlock.createSprite("roundkickleft1");
		kickBlockLeft[1] = StaticTextures.kickBlock.createSprite("roundkickleft2");
		kickBlockLeft[2] = StaticTextures.kickBlock.createSprite("roundkickleft3");
		kickBlockLeft[3] = StaticTextures.kickBlock.createSprite("roundkickleft4");
		kickBlockLeft[4] = StaticTextures.kickBlock.createSprite("roundkickleft5");
		kickBlockLeft[5] = StaticTextures.kickBlock.createSprite("roundkickleft6");
		kickBlockLeft[6] = StaticTextures.kickBlock.createSprite("roundkickleft7");
		kickBlockLeft[7] = StaticTextures.kickBlock.createSprite("roundkickleft8");
		kickBlockLeft[8] = StaticTextures.kickBlock.createSprite("roundkickleft9");
		fallDownHole = new Sprite[17];
		for(int i = 0; i < fallDownHole.length; i++){
			fallDownHole[i] = StaticTextures.character.createSprite("falldownhole"+i);
		}

		spriteToDraw = charMoveDown[animStep];
		currentState = VariableCollection.CharIdleDown;
	}
	public void setPosition(int x, int y) {
		tileX = x;
		tileY = y;
		spriteX = x*64;
		spriteY = y*64;
	}
	public void render(SpriteBatch sb, float r, float b, float g, float a) {
		if (right)
			spriteToDraw.setPosition(spriteX-(spriteToDraw.getWidth()-64), spriteY);
		else if (left)
			spriteToDraw.setPosition(spriteX-8, spriteY);
		else if (up)
			spriteToDraw.setPosition(spriteX, spriteY-64);			
		else
			spriteToDraw.setPosition(spriteX, spriteY);
		spriteToDraw.setColor(r,g,b,a);
		spriteToDraw.draw(sb);
	}
	public void update(Simulation sim) {
		moveTimer += Gdx.graphics.getDeltaTime()*2000;
		right = false;
		left = false;
		if (currentState == VariableCollection.CharIdleDown) {
			spriteToDraw = charStill[0];
		}
		else if (currentState == VariableCollection.CharIdleUp) {
			spriteToDraw = charMoveUp[0];	
		} 
		else if (currentState == VariableCollection.CharIdleLeft) {
			spriteToDraw = hitBlockLeft[0];
		}
		else if (currentState == VariableCollection.CharIdleRight) {
			spriteToDraw = hitBlockRight[0];
		}
		else if (currentState == VariableCollection.CharMoveRight) {
			int step = moveTimer/20;
			animStep2 += step;
			spriteX += 4*step;
			if (spriteX >= tileX*64) {
				currentState = VariableCollection.CharIdleRight;
				locked = false;
				spriteX = tileX*64;
				standingStill(sim);
			}
			moveTimer -= (20*step);
			if (animStep2 > 0 && animStep2 <= 2)
				spriteToDraw = charMoveRight[0];
			else if (animStep2 > 2 && animStep2 <= 4)
				spriteToDraw = charMoveRight[1];
			else if (animStep2 > 4 && animStep2 <= 6)
				spriteToDraw = charMoveRight[2];
			else
				spriteToDraw = charMoveRight[3];
			right = true;
		} else if (currentState == VariableCollection.CharMoveUp) {
			int step = moveTimer/20;
			animStep2 += step;
			spriteY += 4*step;
			if (spriteY >= tileY*64) {
				currentState = VariableCollection.CharIdleUp;
				locked = false;
				spriteY = tileY*64;
				standingStill(sim);
			}
			moveTimer -= (20*step);
			if (animStep2 > 0 && animStep2 <= 4)
				spriteToDraw = charMoveUp[0];
			else if (animStep2 > 4 && animStep2 <= 8)
				spriteToDraw = charMoveUp[1];
			else if (animStep2 > 8 && animStep2 <= 12)
				spriteToDraw = charMoveUp[2];
			else
				spriteToDraw = charMoveUp[2];
		} else if (currentState == VariableCollection.CharMoveLeft) {
			int step = moveTimer/20;
			spriteX -= 4*step;
			animStep2 += step;
			if (spriteX <= tileX*64) {
				currentState = VariableCollection.CharIdleLeft;
				locked = false;
				spriteX = tileX*64;
				standingStill(sim);
			}
			moveTimer -= (20*step);
			if (animStep2 > 0 && animStep2 <= 2)
				spriteToDraw = charMoveLeft[0];
			else if (animStep2 > 2 && animStep2 <= 4)
				spriteToDraw = charMoveLeft[1];
			else if (animStep2 > 4 && animStep2 <= 6)
				spriteToDraw = charMoveLeft[2];
			else{
				spriteToDraw = charMoveLeft[3];
				left = true;
			}
		} else if (currentState == VariableCollection.CharMoveDown) {
			int step = moveTimer/20;
			animStep2 += step;
			spriteY -= 4*step;
			if (spriteY <= tileY*64) {
				currentState = VariableCollection.CharIdleDown;
				locked = false;
				spriteY = tileY*64;
				standingStill(sim);
			}
			moveTimer -= (20*step);
			if (animStep2 > 0 && animStep2 <= 4)
				spriteToDraw = charMoveDown[0];
			else if (animStep2 > 4 && animStep2 <= 8)
				spriteToDraw = charMoveDown[1];
			else if (animStep2 > 8 && animStep2 <= 12)
				spriteToDraw = charMoveDown[2];
			else
				spriteToDraw = charMoveDown[2];
		} else if (currentState == VariableCollection.CharHitLeft) {
			left = true;
			int step = 40;
			if (moveTimer < step)
				spriteToDraw = hitBlockLeft[0];
			else if (moveTimer < step*2)
				spriteToDraw = hitBlockLeft[1];
			else if (moveTimer < step*3)
				spriteToDraw = hitBlockLeft[2];
			else if (moveTimer < step*4)
				spriteToDraw = hitBlockLeft[3];
			else if (moveTimer < step*5)
				spriteToDraw = hitBlockLeft[4];
			else if (moveTimer < step*6)
				spriteToDraw = hitBlockLeft[5];
			else if (moveTimer < step*7)
				spriteToDraw = hitBlockLeft[6];
			else if (moveTimer < step*8)
				spriteToDraw = hitBlockLeft[7];
			else if (moveTimer < step*9) {
				spriteToDraw = hitBlockLeft[8];
			} else {
				left = false;
				sim.map.blocks[tileX-1][tileY].moveLeft(sim);
				spriteToDraw = charStill[0];
				locked = false;
				currentState = VariableCollection.CharIdleLeft;
			}
		} else if (currentState == VariableCollection.CharHitRight) {
			int step = 40;
			if (moveTimer < step)
				spriteToDraw = hitBlockRight[0];
			else if (moveTimer < step*2)
				spriteToDraw = hitBlockRight[1];
			else if (moveTimer < step*3)
				spriteToDraw = hitBlockRight[2];
			else if (moveTimer < step*4)
				spriteToDraw = hitBlockRight[3];
			else if (moveTimer < step*5)
				spriteToDraw = hitBlockRight[4];
			else if (moveTimer < step*6)
				spriteToDraw = hitBlockRight[5];
			else if (moveTimer < step*7)
				spriteToDraw = hitBlockRight[6];
			else if (moveTimer < step*8)
				spriteToDraw = hitBlockRight[7];
			else if (moveTimer < step*9) {
				spriteToDraw = hitBlockRight[8];
			} else {
				sim.map.blocks[tileX+1][tileY].moveRight(sim);
				spriteToDraw = charStill[0];
				locked = false;
				currentState = VariableCollection.CharIdleRight;
			}
		} else if (currentState == VariableCollection.CharHitUp) {
			int step = 80;
			if (moveTimer < step)
				spriteToDraw = hitBlockUp[0];
			else if (moveTimer < step*2)
				spriteToDraw = hitBlockUp[1];
			else if (moveTimer < step*3)
				spriteToDraw = hitBlockUp[2];
			else if (moveTimer < step*4)
				spriteToDraw = hitBlockUp[3];
			else if (moveTimer < step*5)
				spriteToDraw = hitBlockUp[4];
			else if (moveTimer < step*6)
				spriteToDraw = hitBlockUp[5];
			else if (moveTimer < step*7)
				spriteToDraw = hitBlockUp[6];
			else if (moveTimer < step*8)
				spriteToDraw = hitBlockUp[7];
			else {
				sim.map.blocks[tileX][tileY+1].moveUp(sim);
				spriteToDraw = charStill[0];
				locked = false;
				currentState = VariableCollection.CharIdleUp;
			}
		} else if (currentState == VariableCollection.CharKickRight) {
			int step = 80;
			if (moveTimer < step)
				spriteToDraw = kickBlockRight[0];
			else if (moveTimer < step*2)
				spriteToDraw = kickBlockRight[1];
			else if (moveTimer < step*3)
				spriteToDraw = kickBlockRight[2];
			else if (moveTimer < step*4)
				spriteToDraw = kickBlockRight[3];
			else if (moveTimer < step*5)
				spriteToDraw = kickBlockRight[4];
			else if (moveTimer < step*6)
				spriteToDraw = kickBlockRight[5];
			else if (moveTimer < step*7)
				spriteToDraw = kickBlockRight[6];
			else if (moveTimer < step*8)
				spriteToDraw = kickBlockRight[7];
			else if (moveTimer < step*9) {
				spriteToDraw = kickBlockRight[8];
			} else {
				sim.map.blocks[tileX+1][tileY].moveRight(sim);
				spriteToDraw = charStill[0];
				locked = false;
				currentState = VariableCollection.CharIdleRight;
			}
		} else if (currentState == VariableCollection.CharKickLeft) {
			int step = 80;
			left = true;
			if (moveTimer < step)
				spriteToDraw = kickBlockLeft[0];
			else if (moveTimer < step*2)
				spriteToDraw = kickBlockLeft[1];
			else if (moveTimer < step*3)
				spriteToDraw = kickBlockLeft[2];
			else if (moveTimer < step*4)
				spriteToDraw = kickBlockLeft[3];
			else if (moveTimer < step*5)
				spriteToDraw = kickBlockLeft[4];
			else if (moveTimer < step*6)
				spriteToDraw = kickBlockLeft[5];
			else if (moveTimer < step*7)
				spriteToDraw = kickBlockLeft[6];
			else if (moveTimer < step*8)
				spriteToDraw = kickBlockLeft[7];
			else if (moveTimer < step*9) {
				spriteToDraw = kickBlockLeft[8];
			} else {
				left = false;
				sim.map.blocks[tileX-1][tileY].moveLeft(sim);
				spriteToDraw = charStill[0];
				locked = false;
				currentState = VariableCollection.CharIdleLeft;
			}
		} else if (currentState == VariableCollection.CharKickDown) {
			int step = 80;
			up = true;
			if (moveTimer < step)
				spriteToDraw = kickBlockDown[0];
			else if (moveTimer < step*2)
				spriteToDraw = kickBlockDown[1];
			else if (moveTimer < step*3)
				spriteToDraw = kickBlockDown[2];
			else if (moveTimer < step*4)
				spriteToDraw = kickBlockDown[3];
			else if (moveTimer < step*5)
				spriteToDraw = kickBlockDown[4];
			else if (moveTimer < step*6)
				spriteToDraw = kickBlockDown[5];
			else {
				sim.map.blocks[tileX][tileY-1].moveDown(sim);
				spriteToDraw = charStill[0];
				locked = false;
				up = false;
				currentState = VariableCollection.CharIdleDown;
			}
		} else if (currentState == VariableCollection.CharFallDownHole) {
			int step = 80;
			if (moveTimer < step*9)
				spriteToDraw = fallDownHole[0];
			else if (moveTimer < step*10)
				spriteToDraw = fallDownHole[1];
			else if (moveTimer < step*11)
				spriteToDraw = fallDownHole[2];
			else if (moveTimer < step*12)
				spriteToDraw = fallDownHole[3];
			else if (moveTimer < step*13)
				spriteToDraw = fallDownHole[4];
			else if (moveTimer < step*14)
				spriteToDraw = fallDownHole[5];
			else if (moveTimer < step*15)
				spriteToDraw = fallDownHole[6];
			else if (moveTimer < step*16)
				spriteToDraw = fallDownHole[7];
			else if (moveTimer < step*17)
				spriteToDraw = fallDownHole[8];
			else if (moveTimer < step*18)
				spriteToDraw = fallDownHole[9];
			else if (moveTimer < step*19)
				spriteToDraw = fallDownHole[10];
			else if (moveTimer < step*20)
				spriteToDraw = fallDownHole[11];
			else if (moveTimer < step*21)
				spriteToDraw = fallDownHole[12];
			else if (moveTimer < step*22)
				spriteToDraw = fallDownHole[13];
			else if (moveTimer < step*23)
				spriteToDraw = fallDownHole[14];
			else if (moveTimer < step*24)
				spriteToDraw = fallDownHole[15];
			else if (moveTimer < step*25)
				spriteToDraw = fallDownHole[16];
			else {
				if (((Game) sim.main.getState(Main.SCREEN_GAME)).testing)
					((MapEditor) sim.main.getState(Main.SCREEN_MAPEDITOR)).restartTest();
				else if (((Game) sim.main.getState(Main.SCREEN_GAME)).custom)
					((CustomGame) sim.main.getState(Main.SCREEN_LOADFILE)).restartTest();
				else
					sim.restartMap();
				currentState = VariableCollection.CharIdleDown;
				locked = false;
			}
		}
	}
	public void standingStill(Simulation sim) {
		if (sim.map.terrainBlocks[tileX][tileY].type == VariableCollection.hole) {
			currentState = VariableCollection.CharFallDownHole;
			locked = true;
		} 
		if (sim.map.blocks[tileX][tileY].type == VariableCollection.key_red) {
			this.redKeys++;
			sim.map.blocks[tileX][tileY].type = VariableCollection.nothing;
		} else if (sim.map.blocks[tileX][tileY].type == VariableCollection.key_blue) {
			this.blueKeys++;
			sim.map.blocks[tileX][tileY].type = VariableCollection.nothing;
		} else if (sim.map.blocks[tileX][tileY].type == VariableCollection.key_yellow) {
			this.yellowKeys++;
			sim.map.blocks[tileX][tileY].type = VariableCollection.nothing;
		} else if (sim.map.blocks[tileX][tileY].type == VariableCollection.goal) {
			if (((Game) sim.main.getState(Main.SCREEN_GAME)).testing)
				sim.main.setScreen(Main.SCREEN_MAPEDITOR);
			else if (((Game) sim.main.getState(Main.SCREEN_GAME)).custom)
				sim.main.setScreen(Main.SCREEN_LOADFILE);
			else
				sim.setLevel(sim.getNextlevel());
		}
	}
	public void moveRight(Simulation sim) {
		if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.wall || 
				(sim.map.terrainBlocks[tileX+1][tileY].up && 
						(sim.map.terrainBlocks[tileX+1][tileY].type == VariableCollection.pillar_yellow||
								sim.map.terrainBlocks[tileX+1][tileY].type == VariableCollection.pillar_blue))|| 
								sim.map.terrainBlocks[tileX+1][tileY].type == VariableCollection.dirtStuck || 
								sim.map.terrainBlocks[tileX+1][tileY].state == VariableCollection.fillingHole){
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickRight;
			}
			else{
				currentState = VariableCollection.CharHitRight;
			}
			locked = true;
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.nothing || (sim.map.terrainBlocks[tileX+1][tileY].state == VariableCollection.pillardown && !(sim.map.blocks[tileX+1][tileY].type == VariableCollection.moveBlock || sim.map.blocks[tileX+1][tileY].type == VariableCollection.mergeblock))){
			locked = true;
			right = true;
			tileX+=1;
			currentState = VariableCollection.CharMoveRight;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.moveBlock) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickRight;
			}
			else{
				currentState = VariableCollection.CharHitRight;
			}
			locked = true;
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.boulder) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickRight;
			}
			else{
				currentState = VariableCollection.CharHitRight;
			}
			locked = true;
		} 
		else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.mergeblock) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickRight;
			}
			else{
				currentState = VariableCollection.CharHitRight;
			}
			locked = true;
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.key_red || 
				sim.map.blocks[tileX+1][tileY].type == VariableCollection.key_blue || 
				sim.map.blocks[tileX+1][tileY].type == VariableCollection.key_yellow ||
				sim.map.blocks[tileX+1][tileY].type == VariableCollection.goal) {
			locked = true;
			tileX+=1;
			currentState = VariableCollection.CharMoveRight;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.door_red) {
			if(sim.map.blocks[tileX+1][tileY].state == VariableCollection.doorclosed){
				if (redKeys > 0) {
					redKeys--;
					sim.map.blocks[tileX+1][tileY].state = VariableCollection.dooropen;
				}
			}
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.door_blue) {
			if(sim.map.blocks[tileX+1][tileY].state == VariableCollection.doorclosed){
				if (blueKeys > 0) {
					blueKeys--;
					sim.map.blocks[tileX+1][tileY].state = VariableCollection.dooropenB;
				}
			}
		} else if (sim.map.blocks[tileX+1][tileY].type == VariableCollection.door_yellow) {
			if(sim.map.blocks[tileX+1][tileY].state == VariableCollection.doorclosed){ 
				if (yellowKeys > 0) {
					yellowKeys--;
					sim.map.blocks[tileX+1][tileY].state = VariableCollection.dooropenY;
				}
			}
		}
	}
	public void moveLeft(Simulation sim) {
		if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.wall || 
				(sim.map.terrainBlocks[tileX-1][tileY].up &&
						(sim.map.terrainBlocks[tileX-1][tileY].type == VariableCollection.pillar_yellow || 
								sim.map.terrainBlocks[tileX-1][tileY].type == VariableCollection.pillar_blue)) || 
								sim.map.terrainBlocks[tileX-1][tileY].type == VariableCollection.dirtStuck || 
								sim.map.terrainBlocks[tileX-1][tileY].state == VariableCollection.fillingHole){
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickLeft;
			}
			else{
				currentState = VariableCollection.CharHitLeft;
			}
			locked = true;
		} else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.nothing||(sim.map.terrainBlocks[tileX-1][tileY].state == VariableCollection.pillardown && !(sim.map.blocks[tileX-1][tileY].type == VariableCollection.moveBlock || sim.map.blocks[tileX-1][tileY].type == VariableCollection.mergeblock))) {
			locked = true;
			tileX-=1;
			currentState = VariableCollection.CharMoveLeft;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.moveBlock) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickLeft;
			}
			else{
				currentState = VariableCollection.CharHitLeft;
			}
			locked = true;
		} 
		else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.mergeblock) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickLeft;
			}
			else{
				currentState = VariableCollection.CharHitLeft;
			}
			locked = true;
		} 
		else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.boulder) {
			moveTimer = 0;
			if(MathUtils.random(10) < 6){
				currentState = VariableCollection.CharKickLeft;
			}
			else{
				currentState = VariableCollection.CharHitLeft;
			}
			locked = true;
		}else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.key_red || 
				sim.map.blocks[tileX-1][tileY].type == VariableCollection.key_blue || 
				sim.map.blocks[tileX-1][tileY].type == VariableCollection.key_yellow ||
				sim.map.blocks[tileX-1][tileY].type == VariableCollection.goal) {
			locked = true;
			tileX-=1;
			currentState = VariableCollection.CharMoveLeft;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.door_red) {
			if(sim.map.blocks[tileX-1][tileY].state == VariableCollection.doorclosed){
				if (redKeys > 0) {
					redKeys--;
					sim.map.blocks[tileX-1][tileY].state = VariableCollection.dooropen;
				}
			}
		} else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.door_blue) {
			if(sim.map.blocks[tileX-1][tileY].state == VariableCollection.doorclosed){
				if (blueKeys > 0) {
					blueKeys--;
					sim.map.blocks[tileX-1][tileY].state = VariableCollection.dooropenB;
				}
			}
		} else if (sim.map.blocks[tileX-1][tileY].type == VariableCollection.door_yellow) {
			if(sim.map.blocks[tileX-1][tileY].state == VariableCollection.doorclosed){
				if (yellowKeys > 0) {
					yellowKeys--;
					sim.map.blocks[tileX-1][tileY].state = VariableCollection.dooropenY;
				}
			}
		}
	}
	public void moveDown(Simulation sim) {
		if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.wall || 
				(sim.map.terrainBlocks[tileX][tileY-1].up && 
						(sim.map.terrainBlocks[tileX][tileY-1].type == VariableCollection.pillar_yellow || sim.map.terrainBlocks[tileX][tileY-1].type == VariableCollection.pillar_blue)) || 
						sim.map.terrainBlocks[tileX][tileY-1].type == VariableCollection.dirtStuck || 
						sim.map.terrainBlocks[tileX][tileY-1].state == VariableCollection.fillingHole){
			moveTimer = 0;
			currentState = VariableCollection.CharKickDown;
			locked = true;
		} else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.nothing||(sim.map.terrainBlocks[tileX][tileY-1].state == VariableCollection.pillardown && !(sim.map.blocks[tileX][tileY-1].type == VariableCollection.moveBlock || sim.map.blocks[tileX][tileY-1].type == VariableCollection.mergeblock))) {
			locked = true;
			tileY-=1;
			currentState = VariableCollection.CharMoveDown;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.moveBlock) {
			moveTimer = 0;
			currentState = VariableCollection.CharKickDown;
			locked = true;
		} 
		else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.mergeblock) {
			moveTimer = 0;
			currentState = VariableCollection.CharKickDown;
			locked = true;
		} 
		else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.boulder) {
			moveTimer = 0;
			currentState = VariableCollection.CharKickDown;
			locked = true;
		}else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.key_red || 
				sim.map.blocks[tileX][tileY-1].type == VariableCollection.key_blue || 
				sim.map.blocks[tileX][tileY-1].type == VariableCollection.key_yellow ||
				sim.map.blocks[tileX][tileY-1].type == VariableCollection.goal) {
			locked = true;
			tileY-=1;
			currentState = VariableCollection.CharMoveDown;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.door_red) {
			if(sim.map.blocks[tileX][tileY-1].state == VariableCollection.doorclosed){
				if (redKeys > 0) {
					redKeys--;
					sim.map.blocks[tileX][tileY-1].state = VariableCollection.dooropen;
				}
			}
		} else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.door_blue) {
			if(sim.map.blocks[tileX][tileY-1].state == VariableCollection.doorclosed){
				if (blueKeys > 0) {
					blueKeys--;
					sim.map.blocks[tileX][tileY-1].state = VariableCollection.dooropenB;
				}
			}
		} else if (sim.map.blocks[tileX][tileY-1].type == VariableCollection.door_yellow) {
			if(sim.map.blocks[tileX][tileY-1].state == VariableCollection.doorclosed){
				if (yellowKeys > 0) {
					yellowKeys--;
					sim.map.blocks[tileX][tileY-1].state = VariableCollection.dooropenY;
				}
			}
		}
	}
	public void moveUp(Simulation sim) {

		if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.wall || 
				(sim.map.terrainBlocks[tileX][tileY+1].up && 
						(sim.map.terrainBlocks[tileX][tileY+1].type == VariableCollection.pillar_yellow ||
								sim.map.terrainBlocks[tileX][tileY+1].type == VariableCollection.pillar_blue)) || 
								sim.map.terrainBlocks[tileX][tileY+1].type == VariableCollection.dirtStuck || 
								sim.map.terrainBlocks[tileX][tileY+1].state == VariableCollection.fillingHole){
			moveTimer = 0;
			currentState = VariableCollection.CharHitUp;
			locked = true;
		} else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.nothing||(sim.map.terrainBlocks[tileX][tileY+1].state == VariableCollection.pillardown && !(sim.map.blocks[tileX][tileY+1].type == VariableCollection.moveBlock || sim.map.blocks[tileX][tileY+1].type == VariableCollection.mergeblock))) {
			locked = true;
			tileY+=1;
			currentState = VariableCollection.CharMoveUp;
			moveTimer = 0;
			animStep2 = 0;
		}else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.moveBlock) {
			moveTimer = 0;
			currentState = VariableCollection.CharHitUp;
			locked = true;
		}
		else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.mergeblock) {
			moveTimer = 0;
			currentState = VariableCollection.CharHitUp;
			locked = true;
		}
		else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.boulder) {
			moveTimer = 0;
			currentState = VariableCollection.CharHitUp;
			locked = true;
		}else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.key_red || 
				sim.map.blocks[tileX][tileY+1].type == VariableCollection.key_blue || 
				sim.map.blocks[tileX][tileY+1].type == VariableCollection.key_yellow ||
				sim.map.blocks[tileX][tileY+1].type == VariableCollection.goal) {
			locked = true;
			tileY+=1;
			currentState = VariableCollection.CharMoveUp;
			moveTimer = 0;
			animStep2 = 0;
		} else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.door_red) {
			if(sim.map.blocks[tileX][tileY+1].state == VariableCollection.doorclosed){
				if (redKeys > 0) {
					redKeys--;
					sim.map.blocks[tileX][tileY+1].state = VariableCollection.dooropen;
				}
			}
		} else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.door_blue) {
			if(sim.map.blocks[tileX][tileY+1].state == VariableCollection.doorclosed){
				if (blueKeys > 0) {
					blueKeys--;
					sim.map.blocks[tileX][tileY+1].state = VariableCollection.dooropenB;
				}
			}
		} else if (sim.map.blocks[tileX][tileY+1].type == VariableCollection.door_yellow) {
			if(sim.map.blocks[tileX][tileY+1].state == VariableCollection.doorclosed){
				if (yellowKeys > 0) {
					yellowKeys--;
					sim.map.blocks[tileX][tileY+1].state = VariableCollection.dooropenY;
				}
			}
		}
	}
}